package com.yuke.cloud.service.cmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.cmc.entity.CmsAppNavi;
import com.yuke.cloud.service.cmc.service.CmsAppNaviService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* Created by wg on 2019/06/21.
*/
@Api(value = "WEB - CmsAppNaviController", tags = "处理CmsAppNavi相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/app_navi")
public class CmsAppNaviController extends BaseController {
    @Resource
    private CmsAppNaviService cmsAppNaviService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsAppNavi", value = "该数据记录的POJO") @RequestBody CmsAppNavi cmsAppNavi) {
        cmsAppNavi.setUpdateUser(getLoginAuthDto().getUserId());
        cmsAppNavi.setUpdateTime(new Date());
        cmsAppNaviService.save(cmsAppNavi);
        return WrapMapper.ok(cmsAppNavi.getNaviId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsAppNaviService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsAppNavi", value = "需要修改的内容") @RequestBody CmsAppNavi cmsAppNavi) {
        cmsAppNavi.setUpdateUser(getLoginAuthDto().getUserId());
        cmsAppNavi.setUpdateTime(new Date());
        cmsAppNaviService.update(cmsAppNavi);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsAppNavi> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsAppNavi cmsAppNavi = cmsAppNaviService.findById(id);
        return WrapMapper.ok(cmsAppNavi);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsAppNavi>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                              @ApiParam(name = "naviType", value = "naviType") @RequestParam(required = false) Byte naviType) {
        PageHelper.startPage(page, size);
        PageHelper.orderBy("sort");
        if (naviType != null) {
            CmsAppNavi cmsAppNavi = new CmsAppNavi();
            cmsAppNavi.setNaviType(naviType);
            List<CmsAppNavi> list = cmsAppNaviService.select(cmsAppNavi);
            PageInfo pageInfo = new PageInfo(list);
            return WrapMapper.ok(pageInfo);
        } else {
            List<CmsAppNavi> list = cmsAppNaviService.findAll();
            PageInfo pageInfo = new PageInfo(list);
            return WrapMapper.ok(pageInfo);
        }
    }

    @DeleteMapping("/ids")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    public Wrapper deleteByIds(@ApiParam(name = "ids", value = "要删除的导航按钮ID集") @RequestBody String naviIds) {
        cmsAppNaviService.deleteByIds(naviIds);
        return WrapMapper.ok();
    }

    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据导航按钮名称检查唯一性")
    public Wrapper ifExist(@ApiParam(name = "naviName", value = "cmsAppNavi") @RequestBody CmsAppNavi cmsAppNavi) {
        int count = cmsAppNaviService.selectCount(cmsAppNavi);
        if (cmsAppNavi.getNaviId() != null) {  // 排除自身
            count = 0;
        }

        return WrapMapper.ok(count > 0);
    }

    @PutMapping("/batchUpdate")
    @ApiOperation(httpMethod = "PUT", value = "批量修改")
    public Wrapper batchUpdate(@RequestBody List<CmsAppNavi> cmsAppNavis) {
        for (CmsAppNavi cmsAppNavi : cmsAppNavis) {
            cmsAppNavi.setUpdateUser(getLoginAuthDto().getUserId());
            cmsAppNavi.setUpdateTime(new Date());
            cmsAppNaviService.update(cmsAppNavi);
        }
        return WrapMapper.ok();
    }
}
